<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" style="width:300px" placeholder="用户名、用户操作"  clearable></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search" icon="el-icon-search">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="dataList" border highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">

			<el-table-column type="selection" width="50">
			</el-table-column>
			<!--<el-table-column type="index" width="60">-->
			<!--</el-table-column>-->
			<el-table-column prop="id" label="ID" type="index" width="40">
			</el-table-column>
			<el-table-column prop="username" label="用户名" width="120" >
			</el-table-column>
			<el-table-column prop="operation" label="用户操作" min-width="200" >
			</el-table-column>
			<el-table-column prop="method" label="请求方法" min-width="200" >
			</el-table-column>
			<el-table-column prop="params" label="请求参数" min-width="200" >
			</el-table-column>
			<el-table-column prop="time" label="执行时长(毫秒)" min-width="150" sortable>
			</el-table-column>
			<el-table-column prop="ip" label="IP地址" min-width="100" >
			</el-table-column>
			<el-table-column prop="createDate" label="创建时间" min-width="200" sortable>
			</el-table-column>

		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="total, sizes, prev, pager, next, jumper"
						   @size-change="handleSizeChange"
						   @current-change="handleCurrentChange"
						   :page-sizes="pageSizes"
						   :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

	</section>
</template>

<script>

    export default {
        data() {
            return {
                filters: {
                    keyword: ''
                },

                listLoading: false, //是否显示加载动画

                dataList: [], //数据列表(日志)

                total: 0,
                pageIndex: 1, //页码
                pageSize:this.CONSTANT.PAGE_SIZE, //分页大小
                pageSizes:this.CONSTANT.PAGE_SIZES, //分页大小选择列表

                sels: [],//列表选中列

            }
        },
        methods: {
            selsChange: function (sels) { //保存复选框选中的记录
                this.sels = sels;
            },
            handleSizeChange(val) { //改变分页大小
                this.pageSize = val;
                this.getDataList();
            },
            handleCurrentChange(val) { //页码跳转
                this.pageIndex = val;
                this.getDataList();
            },
            search(){
                if(this.filters.keyword==''){
                    this.$message('请输入搜索内容');
                    return;
                }
                this.getDataList();
            },
            getDataList() { //获取数据列表（日志）
                this.listLoading = true;

                let para = {
                    pageIndex: this.pageIndex,
                    pageSize:this.pageSize,
                    keyword: this.filters.keyword
                };

                this.$http.post('/api/sysLog/list/find', para).then(res => {
                    this.listLoading = false;

                    this.total = res.data.total;
                    this.dataList = res.data.data;

                }).catch((error) => {
                    this.listLoading = false;
                    if(error) console.log(error);
                });
            },
            batchRemove: function () { //批量删除
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    let para = { ids: ids };
                    this.$http.get('/api/sysLog/list/remove/'+para.ids).then((res) => {
                        if (res.data.code == 0) {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: '删除失败',

                                type: 'error'
                            });
                        }

                        this.getDataList();
                    });
                }).catch(() => {
                    if(error) console.log(error);
                });
            }
        },
        mounted() {
            this.getDataList(); //进入页面就加载数据
        }
    }

</script>
